package cn.manjuu.touhou.activity.fragment;

import java.util.Random;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import cn.manjuu.touhou.APP;
import cn.manjuu.touhou.R;
import cn.manjuu.touhou.engine.CardEngine;
import cn.manjuu.touhou.engine.SoundEngine;
import cn.manjuu.touhou.interfaces.OnBaseInfoChangeListener;
import cn.manjuu.touhou.util.Looger;

public class ToyMachineFragment extends Fragment implements OnClickListener,
		OnTouchListener {

	private ImageView iv_toy_machine;
	private ImageView iv_pair_card;
	private View mMainView;
	private OnBaseInfoChangeListener mListener;

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		try {
			mListener = (OnBaseInfoChangeListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ "must implement OnBaseInfoChangeListener");
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mMainView = inflater.inflate(R.layout.toy_machine, null);
		findView();
		registerListener();
		return mMainView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		init();
	}

	private void init() {
		viewGone();
		iv_toy_machine.setVisibility(View.VISIBLE);
	}

	private void viewGone() {
		iv_toy_machine.setVisibility(View.GONE);
		iv_pair_card.setVisibility(View.GONE);
	}

	private void findView() {
		iv_toy_machine = (ImageView) mMainView
				.findViewById(R.id.iv_toy_machine);
		iv_pair_card = (ImageView) mMainView.findViewById(R.id.iv_pair_card);
	}

	private void registerListener() {
		// TODO Auto-generated method stub
		mMainView.setOnTouchListener(this);
		iv_toy_machine.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.iv_toy_machine:
			SoundEngine.getInstance().playEffect(R.raw.se_buy);

			Random random = new Random();
			int nextInt = random.nextInt(14);
			Looger.i(APP.MainActivity, "nextInt: " + nextInt);
			viewGone();
			iv_pair_card.setVisibility(View.VISIBLE);
			iv_pair_card.setImageResource(R.drawable.pair_card_00 + nextInt);

			long addCard = CardEngine.getInstance().addCard(nextInt);
			if (0 != addCard) {
				mListener.onBaseInfoChangeListener();
			}
			break;
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		viewGone();
		iv_toy_machine.setVisibility(View.VISIBLE);
		CardEngine.getInstance().getAllCard();
		return false;
	}

}
